package com.jerei.web.utils.shiro;

import com.jerei.plugin.api.IAdminuserService;
import com.jerei.plugin.api.IMenuDetailService;
import com.jerei.plugin.api.IRoleService;
import com.jerei.plugin.entity.WcmCommonAdminuser;
import com.jerei.plugin.entity.WcmCommonRole;
import com.jerei.plugin.exceptions.PluginNotInstallException;
import com.jerei.plugin.utils.Plugins;
import com.jerei.web.utils.UtilsPluginManager;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by sunfeihu on 2016/10/21.
 */
public class LoginAuthorizingRealm extends AuthorizingRealm {

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        WcmCommonAdminuser adminuser = (WcmCommonAdminuser) SecurityUtils.getSubject().getPrincipal();
        if (null != adminuser) {
            SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
            try {
                IRoleService iRoleService = UtilsPluginManager.getExtension(IRoleService.class, Plugins.COMMON_ROLE);
                List<WcmCommonRole> roles = iRoleService.findRolesByAdminuser(adminuser.getId());
                if (null != roles) {
                    //根据角色查询所有角色
                    Set<String> sets = new HashSet<String>();
                    Long[] roleIds = new Long[roles.size()];
                    WcmCommonRole role = null;
                    for (int i = 0; i < roles.size(); i++) {
                        role = roles.get(i);
                        sets.add(role.getName());
                        roleIds[i] = role.getId();
                    }
                    info.setRoles(sets);
                    IMenuDetailService iMenuDetailService = UtilsPluginManager.getExtension(IMenuDetailService.class, Plugins.COMMON_MENU);
                    sets = iMenuDetailService.findPermissionsString(roleIds);
                    //info.setStringPermissions(sets);
                    info.addStringPermission("*:*:*");
                }
            } catch (PluginNotInstallException e) {
                e.printStackTrace();
            }
            return info;
        }
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        IAdminuserService iAdminuserService = null;
        try {
            iAdminuserService = UtilsPluginManager.getExtension(IAdminuserService.class, Plugins.COMMON_ADMINUSER);
        } catch (PluginNotInstallException e) {
            e.printStackTrace();
        }
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        WcmCommonAdminuser user = iAdminuserService.getAdminuserByUsern(token.getUsername());
        //如果用户不存在
        if (null == user) {
            throw new UnknownAccountException();
        }
        //如果是禁用状态
        if (null != user && user.getState().equals(7L)) {
            //抛出账户被禁用异常
            throw new DisabledAccountException();
        }
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), getName());
        info.setCredentialsSalt(ByteSource.Util.bytes(token.getUsername()));
        return info;
    }


}